class BlueprintSystem {
    constructor() {
        this.nodes = [];
        this.connections = [];
        this.variables = {}; // 存储变量 {name: {type, value, id}}
        this.nextNodeId = 1;
        this.nextVarId = 1;
        this.nextNodeId = 1;
    }

    generateNodeId() {
        return `node_${this.nextNodeId++}`;
    }

    addNode(node) {
        this.nodes.push(node);
        return node;
    }

    removeNode(nodeId) {
        this.nodes = this.nodes.filter(node => node.id !== nodeId);
        this.removeConnectionsForNode(nodeId);
    }

    addConnection(sourceNodeId, sourcePin, targetNodeId, targetPin) {
        const connection = {
            id: `conn_${Date.now()}`,
            sourceNode: sourceNodeId,
            sourcePin,
            targetNode: targetNodeId,
            targetPin
        };
        this.connections.push(connection);
        return connection;
    }

    removeConnection(connectionId) {
        this.connections = this.connections.filter(conn => conn.id !== connectionId);
    }

    removeConnectionsForNode(nodeId) {
        this.connections = this.connections.filter(
            conn => conn.sourceNode !== nodeId && conn.targetNode !== nodeId
        );
    }

    getNode(nodeId) {
        return this.nodes.find(node => node.id === nodeId);
    }

    getVariable(varId) {
        return this.variables[varId];
    }

    dVariable(name, type, initialValue = null) {
        const varId = `var_${this.nextVarId++}`;
        this.variables[varId] = {
            id: varId,
            name,
            type,
            value: initialValue
        };
        return varId;
    }

    removeVariable(varId) {
        delete this.variables[varId];
        // 移除所有与该变量相关的节点
        this.nodes = this.nodes.filter(node =>
            !(node instanceof VariableNode && node.varId === varId) &&
            !(node instanceof GetVariableNode && node.varId === varId) &&
            !(node instanceof SetVariableNode && node.varId === varId)
        );
    }

    updateVariable(varId, newValue) {
        if (this.variables[varId]) {
            this.variables[varId].value = newValue;
        }
    }

    getAllVariables() {
        return Object.values(this.variables);
    }

// 检查变量名是否已存在
    isVariableNameTaken(name) {
        return Object.values(this.variables).some(v => v.name === name);
    }

// 修改变量名
    renameVariable(varId, newName) {
        if (this.variables[varId]) {
            this.variables[varId].name = newName;
            // 更新所有相关节点标题
            this.nodes.forEach(node => {
                if (node instanceof VariableNode && node.varId === varId) {
                    if (node instanceof GetVariableNode) {
                        node.title = `Get ${newName}`;
                    } else if (node instanceof SetVariableNode) {
                        node.title = `Set ${newName}`;
                    } else if (node instanceof VariableGetterNode) {
                        node.title = newName;
                    }
                }
            });
            return true;
        }
        return false;
    }

// 修改变量类型
    changeVariableType(varId, newType) {
        if (this.variables[varId]) {
            this.variables[varId].type = newType;
            // 更新所有相关节点的引脚类型
            this.nodes.forEach(node => {
                if (node instanceof VariableNode && node.varId === varId) {
                    if (node instanceof GetVariableNode || node instanceof VariableGetterNode) {
                        const outputPin = node.outputPins.find(p => p.name === "Value");
                        if (outputPin) outputPin.type = newType;
                    } else if (node instanceof SetVariableNode) {
                        const inputPin = node.inputPins.find(p => p.name === "Value");
                        if (inputPin) inputPin.type = newType;
                    }
                }
            });
            return true;
        }
        return false;
    }

}
